<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>角色权限</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
  <link rel="stylesheet" th:href="@{/ztree/css/metroStyle/metroStyle.css}" media="all">
</head>
<body>

<div class="layui-form" lay-filter="role-resource-form-edit" id="role-resource-form-edit" style="padding: 20px 30px 0 0;">
  <input type="hidden" name="roleId" th:value="${roleId}"/>
  <input type="hidden" id="resIds" name="resIds"/>
  <div id="resTree" class="ztree"></div>


  <div class="layui-form-item layui-hide">
    <input type="button" lay-submit lay-filter="LAY-role-resource-front-submit" id="LAY-role-resource-front-submit" value="确认">
  </div>
</div>

<script th:src="@{/ztree/js/jquery-1.4.4.min.js}"></script>
<script th:src="@{/ztree/js/jquery.ztree.all.min.js}"></script>
<script th:src="@{/ztree/js/jquery.ztree.exhide.min.js}"></script>
<script th:src="@{/layui/layui.js}"></script>
<script th:src="@{/config.js}"></script>
<script th:src="@{/adminui/src/modules/view.js}"></script>
<script th:src="@{/adminui/src/modules/admin.js}"></script>
<script th:inline="javascript">
  let resources = [[${resources}]];
</script>
<script>

  $(function () {
    let setting = {
      callback: {
        onCheck: zTreeOnCheck
      },
      check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: {"Y": "ps", "N": "ps"}
      },
      data: {
        key: {
          name: "title"
          // level: "level"
        },
        simpleData: {
          enable: true,
          idKey: "id",
          pIdKey: "pId",
          rootPId: "root"
        }
      },
      //页面上的显示效果
      view: {
        // addHoverDom: addHoverDom, //当鼠标移动到节点上时，显示用户自定义控件
        // removeHoverDom: removeHoverDom //离开节点时的操作
      },
      edit: {
        // enable: true, //单独设置为true时，可加载修改、删除图标
        editNameSelectAll: true
        // showRemoveBtn: showRemoveBtn,
        // showRenameBtn: showRenameBtn
      }
    };
    let treeNodes;
    layui.admin.req({
      type: "post",
      url: '/sys/resource/getResourceTree',
      done: function (res) {
        treeNodes = res.data;
        $.fn.zTree.init($("#resTree"), setting, treeNodes);
        let treeObj = $.fn.zTree.getZTreeObj("resTree");
        // treeObj.expandAll(true);
        let nodes = treeObj.getNodes();
        if (nodes && nodes.length > 0) {
          //默认展开第一级节点
          treeObj.expandNode(nodes[0], true, false, false);
        }

        let resIds = [];
        //回显选中的
        for (let i = 0; i < resources.length; i++) {
          let node = treeObj.getNodeByParam("id", resources[i].resId, null);
          if (node) {
            //选中节点
            treeObj.checkNode(node, true, false);
            //展开选中节点
            treeObj.expandNode(node, true, true, true);

            resIds.push(resources[i].resId);
          }
        }
        $("#resIds").val(resIds.join(","));
      },
      error: function () {
        layer.msg('加载失败', {icon: 7, time: 3000, shade: [0.3, '#000', true]});
      }
    });
  });

  //菜单树check事件
  function zTreeOnCheck(event, treeId, treeNode) {
    let treeObj = $.fn.zTree.getZTreeObj("resTree");
    let nodes = treeObj.getCheckedNodes(true);
    let resIds = [];
    for (let i = 0; i < nodes.length; i++) {
      resIds.push(nodes[i].id);
    }
    $("#resIds").val(resIds.join(","));
  }
</script>
</body>
</html>